package com.zegome.game.oanquan.scene.play;

import java.util.ArrayList;

import com.badlogic.gdx.math.Vector2;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.entity.text.Text;
import com.zegome.game.oanquan.GameCenter;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class PondData {
	// ===========================================================
	// Constants
	// ===========================================================
	public static final int WIDTH = 100;
	public static final int HEIGHT = 100;
	
	public static final int SIZE = 30;
	
	// ===========================================================
	// Fields
	// ===========================================================
	private ArrayList<Sprite> mStoneSprites;
	
	public final Vector2 mPosition;
	public Text mText;
	
	final int mContructor = (WIDTH - SIZE) / 2;
	
	private final int mFlag;
	private final int mIndex;
	
	public int mStoneDevide = 0;
	
	public boolean isRich;
	private final boolean isCourting;
	public boolean isDeviding;
	private boolean isEated;
	
	private int mCountStone;
	
	// ===========================================================
	// Constructor
	// ===========================================================
	public PondData(final int flag, final int index, final boolean isCourting) {
		mFlag = flag;
		mIndex = index;
		this.isCourting = isCourting;
		
		float x;
		float y;
		if (index < 6) {
			x = GameCenter.BOARD_X + index * WIDTH;
			y = GameCenter.BOARD_Y;
		} else {
			x = GameCenter.BOARD_X + (BoardManager.TOTAL_POND - index) * WIDTH;
			y = GameCenter.BOARD_Y + flag * HEIGHT;
		}
		
		mPosition = new Vector2(x, y);
		reset();
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public boolean isEating() {
		if (!isCourting) return true;
		if (!isEated) {
			if (mCountStone >= GameCenter.COURTING) return true;
			return false;
		}
		return true;
	}
	
	public void startDevide() {
		isDeviding = true;
		mStoneDevide = mCountStone;
	}
	
	public void finishDevide() {
		isDeviding = false;
	}
	
	public void setText(final Text text) {
		mText = text;
	}
	
	public int getFlag() {
		return mFlag;
	}
	
	public boolean isCourting() {
		return isCourting;
	}
	
	public void setStone(final int quantity) {
		mCountStone = quantity;
	}
	
	public int getStone() {
		return mCountStone;
	}
	
	public void setEated(final boolean isEated) {
		this.isEated = isEated;
	}
	
	public boolean isEated() {
		return isEated;
	}
	
	public int getIndex() {
		return mIndex;
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public void addPosition(final Sprite stone) {
		float x = GameCenter.GAME_RANDOM.nextInt(SIZE) + mPosition.x;
		float y = GameCenter.GAME_RANDOM.nextInt(SIZE) + mPosition.y;
		
		if (isCourting && mIndex == 0) {
			x += 40;
			y += 80;
		} else if (isCourting && mIndex == 6) {
			x += 10;
			y -= 20;
		} else {
			x += 20;
			y += 25;
		}
		
		stone.setPosition(x, y);
	}
	
	public Vector2 createPosition() {
		final float x = GameCenter.GAME_RANDOM.nextInt(50) + mPosition.x + 15;
		final float y = GameCenter.GAME_RANDOM.nextInt(50) + mPosition.y + 20;
		return new Vector2(x, y);
	}
	
	public void attachStone(final Sprite stone) {
		if (mStoneSprites == null) {
			mStoneSprites = new ArrayList<Sprite>();
		}
		mStoneSprites.add(stone);
		mCountStone++;
		mText.setText(""+mCountStone);
	}
	
	public void attachStone(final Sprite...sprites) {
		for (int i = 0; i < sprites.length; i++) {
			attachStone(sprites[i]);
		}
	}
	
	public Sprite removeStone() {
		if (mStoneSprites == null) {
			throw new IllegalArgumentException("mStoneSprites must be different from null");
		}
		if (mStoneSprites.size() > 0) {
			mCountStone--;
			mText.setText(""+mCountStone);
			return mStoneSprites.remove(0);
		}
		return null;
	}
	
	public Sprite[] eated() {
		if (isCourting) {
			isEated = true;
		}
		final Sprite[] sprites = new Sprite[mStoneSprites.size()];
		for (int i = 0; i < sprites.length; i++) {
			sprites[i] = mStoneSprites.get(i);
		}
		clear();
		return sprites;
	}
	
	public void clear() {
		if (mStoneSprites == null) {
			return;
		}
		mStoneSprites.clear();
		mCountStone = 0;
	}

	public boolean contains(final float x, final float y) {
		if (mPosition.x < x && mPosition.x + WIDTH > x 
				&& mPosition.y < y && mPosition.y + HEIGHT > y)
			return true;
		return false;
	}
	
	public void resetText() {
		mText.setText(""+mCountStone);
	}
	
	public void reset() {
		isDeviding = false;
		isEated = false;
		mCountStone = 0;
		isRich = false;
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
